package com.hg.wap.db.dao;


import java.util.HashMap;
import java.util.Map;

import org.jdiy.core.Args;
import org.jdiy.core.Rs;

import com.hg.util.db.IDao;
import com.hg.wap.db.dto.UserDTO;
import com.hg.wap.db.dto.UserLoginDTO;

public class UserDAO extends IDao
{
	String userTable = db_prefix+"user";
	String fx_reward_link_record_table = db_prefix+"fx_reward_link_record";
	
	private Map<String, String> encapsulationUser(Rs rs){
		Map<String, String> userMap = new HashMap<String, String>();
		userMap.put("id", rs.get("id"));
		userMap.put("user_name", rs.get("user_name"));
		userMap.put("user_pwd", rs.get("user_pwd"));
		userMap.put("create_time", rs.get("create_time"));
		userMap.put("update_time", rs.get("update_time"));
		/*userMap.put("group_id", rs.get("group_id"));
		userMap.put("email", rs.get("email"));
		userMap.put("mobile", rs.get("mobile"));
		userMap.put("score", rs.get("score"));
		userMap.put("total_score", rs.get("total_score"));
		userMap.put("money", rs.get("money"));
		userMap.put("code", rs.get("code"));
		userMap.put("wx_openid", rs.get("wx_openid"));
		userMap.put("is_delete", rs.get("is_delete"));*/
		return userMap;
	}
	public UserLoginDTO findByMobile(String mobile) {
		Args args = null;
		Rs rs = null;
		UserLoginDTO dto = null;
		try {
			args = new Args(userTable, "mobile = " + mobile);
			rs = dao.rs(args);
			if (!rs.isNull()) {
				dto=this.encapsulation(rs);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} catch (Error e) {
			e.printStackTrace();
		}

		return dto;
	}	
	
	public UserLoginDTO create(UserLoginDTO dto){
		Rs rs=null;
		try {
			rs = new Rs(userTable);
			rs.set("user_name", dto.getUser_name());
			rs.set("user_pwd", dto.getUser_pwd());
			rs.set("mobile", dto.getMobile());
			rs.set("is_vip", dto.getIs_vip());
			rs.set("create_time", dto.getCreate_time());
			rs.set("update_time", dto.getCreate_time());
			rs.set("login_ip", dto.getLogin_ip());
			rs.set("is_effect", "1");
			rs.set("is_delete", "0");
			rs.set("is_tmp", "0");
			rs.set("wx_openid", "");
			dao.save(rs);
			return this.encapsulation(rs);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * 根据openid检查该用户是否存在
	 * @param openid
	 * @return
	 */
	public Map<String, String> checkUser(String openid){
		Map<String, String> userMap = new HashMap<String, String>();
		Args args = new Args(db_prefix+"user");
		args.setFilter("1=1"
				+ " and wx_openid='"+openid+"'");
		Rs rs = dao.rs(args);
		if(rs.isNull()){
			userMap.put("err_code", "0");	//还没有改用户
		}else{
			userMap=encapsulationUser(rs);
			userMap.put("err_code", "1992");	//表示未空
		}
		
		
		return userMap;
	}
	
	/**
	 * 自动创建用户
	 * @param map
	 * @return
	 */
	public Map<String, String> autoCreateUser(Map<String, String> map){
		Map<String, String> userMap = new HashMap<String, String>();
		Rs rs = new Rs(db_prefix+"user");
		rs.set("user_name", map.get("user_name"));
		rs.set("user_pwd", map.get("user_pwd"));
		rs.set("create_time", map.get("create_time"));
		rs.set("update_time", map.get("update_time"));
		rs.set("wx_openid", map.get("wx_openid"));
		rs.set("sex", map.get("sex"));
		if(!"".equals(map.get("headimgurl"))){
			rs.set("user_pic", map.get("headimgurl"));
		}
		dao.save(rs);
		if(rs.getString("user_name").equals(rs.getString("wx_openid"))){
			dao.exec("update "+db_prefix+"user"
					+ "	set user_name='"+rs.get("id")+"' where id="+rs.get("id"));
			rs.set("user_name", rs.get("id"));
		}
		userMap=encapsulationUser(rs);
		return userMap;
	}
	
	
	public UserDTO findByUsername(String username) {
		Args args = null;
		Rs rs = null;
		UserDTO dto = null;
		try {
			args = new Args(userTable, "user_name='" + username + "'");
			rs = dao.rs(args);
			if (!rs.isNull()) {
				
				dto = new UserDTO();
				dto.setId(rs.getInt("id"));
				dto.setUser_name(rs.getString("user_name"));
				dto.setUser_pwd(rs.getString("user_pwd"));
				dto.setEmail(rs.getString("email"));
				dto.setMobile(rs.getString("mobile"));
				dto.setByear(rs.getInt("byear"));
				dto.setBmonth(rs.getInt("bmonth"));
				dto.setBday(rs.getInt("bday"));
				dto.setMoney(rs.getDecimal("money"));
				dto.setFx_money(rs.getDecimal("fx_money"));
				dto.setScore(rs.getDecimal("score"));
				if(rs.getString("wx_openid")==null){
					dto.setWx_openid("");
				}else{					
					dto.setWx_openid(rs.getString("wx_openid"));					
				}
				if(rs.getString("user_pic")==null){
					dto.setUser_pic("");
				}else{
					
					dto.setUser_pic(rs.getString("user_pic"));					
				}
				dto.setWechat_id(rs.getString("wechat_id"));
				//System.out.println("wechat_id is "+rs.getString("wechat_id"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} catch (Error e) {
			e.printStackTrace();
		}

		return dto;
	}
	
	
	/**
	 * 登录获取的用户ID
	 * @param openid
	 * @param user_id
	 * @return
	 */
	public UserLoginDTO getRow(String user_id){
		Args args = new Args(userTable);
		args.setFilter("id='"+user_id+"'");
		Rs rs = dao.rs(args);
		if(rs.isNull()){
			return null;
		}else{
			UserLoginDTO dto = this.encapsulation(rs);
			return dto;
		}
		
	}
	
	/**
	 * 登录获取的用户ID
	 * @param openid
	 * @param user_id
	 * @return
	 */
	public UserLoginDTO getRow(String openid,String user_id){
		Args args = new Args(userTable);
		args.setFilter("wx_openid='"+openid+"'");
		Rs rs = dao.rs(args);
		if(rs.isNull()){
			return null;
		}else{
			UserLoginDTO dto = this.encapsulation(rs);
			return dto;
		}
		
	}
	
	public UserLoginDTO encapsulation(Rs rs){
		UserLoginDTO dto = new UserLoginDTO();
		
		dto.setFx_mall_bg(rs.getString("fx_mall_bg"));
		dto.setIs_daren(rs.getInt("is_daren"));
		dto.setVerify(rs.getString("verify"));
		dto.setYpoint(rs.getString("ypoint"));
		dto.setScore(rs.getDouble("score"));
		dto.setFx_money(rs.getDouble("fx_money"));
		dto.setFocus_count(rs.getInt("focus_count"));
		dto.setBound_vip_num(rs.getInt("bound_vip_num"));
		dto.setLocate_time(rs.getInt("locate_time"));
		dto.setRenren_id(rs.getString("renren_id"));
		dto.setT_name(rs.getString("t_name"));
		dto.setIs_agent(rs.getInt("is_agent"));
		dto.setProvince_id(rs.getInt("province_id"));
		dto.setT_openkey(rs.getString("t_openkey"));
		dto.setWechat_id(rs.getString("wechat_id"));
		dto.setAvatar(rs.getString("avatar"));
		dto.setIs_syn_sina(rs.getInt("is_syn_sina"));
		dto.setM_left_times(rs.getInt("m_left_times"));
		dto.setUser_name(rs.getString("user_name"));
		dto.setTencent_id(rs.getString("tencent_id"));
		dto.setReal_name(rs.getString("real_name"));
		dto.setIs_invite(rs.getInt("is_invite"));
		dto.setSina_app_key(rs.getString("sina_app_key"));
		dto.setReferer(rs.getString("referer"));
		dto.setUser_level(rs.getInt("user_level"));
		dto.setSupplier_account_id(rs.getInt("supplier_account_id"));
		dto.setByear(rs.getInt("byear"));
		dto.setInsite_count(rs.getInt("insite_count"));
		dto.setLottery_verify(rs.getString("lottery_verify"));
		dto.setIs_delete(rs.getInt("is_delete"));
		dto.setPassword_verify(rs.getString("password_verify"));
		dto.setLevel_id(rs.getInt("level_id"));
		dto.setTencent_app_secret(rs.getString("tencent_app_secret"));
		dto.setDevice_token(rs.getString("device_token"));
		dto.setMoney(rs.getDouble("money"));
		dto.setMy_intro(rs.getString("my_intro"));
		dto.setXpoint(rs.getString("xpoint"));
		dto.setFav_count(rs.getInt("fav_count"));
		dto.setFx_total_balance(rs.getDouble("fx_total_balance"));
		dto.setTopic_count(rs.getInt("topic_count"));
		dto.setIs_effect(rs.getInt("is_effect"));
		dto.setVip_time(rs.getInt("vip_time"));
		dto.setPid4(rs.getInt("pid4"));
		dto.setPid3(rs.getInt("pid3"));
		dto.setPoint(rs.getInt("point"));
		dto.setPid6(rs.getInt("pid6"));
		dto.setPoster_url(rs.getString("poster_url"));
		dto.setPid5(rs.getInt("pid5"));
		dto.setPid8(rs.getInt("pid8"));
		dto.setPid7(rs.getInt("pid7"));
		dto.setFx_level(rs.getInt("fx_level"));
		dto.setOutsite_count(rs.getInt("outsite_count"));
		dto.setPid9(rs.getInt("pid9"));
		dto.setPid2(rs.getInt("pid2"));
		dto.setPid1(rs.getInt("pid1"));
		dto.setIntegrate_id(rs.getInt("integrate_id"));
		dto.setIs_vip(rs.getInt("is_vip"));
		dto.setTotal_score(rs.getDouble("total_score"));
		dto.setT_openid(rs.getString("t_openid"));
		dto.setUnionid(rs.getString("unionid"));
		dto.setDp_count(rs.getInt("dp_count"));
		dto.setBind_vip_num(rs.getInt("bind_vip_num"));
		dto.setLogin_time(rs.getInt("login_time"));
		dto.setTencent_app_key(rs.getString("tencent_app_key"));
		dto.setLogin_pay_time(rs.getInt("login_pay_time"));
		dto.setLottery_mobile(rs.getString("lottery_mobile"));
		dto.setGroup_id(rs.getInt("group_id"));
		dto.setAgent_id(rs.getInt("agent_id"));
		dto.setCode(rs.getString("code"));
		dto.setFaved_count(rs.getInt("faved_count"));
		dto.setLogin_ip(rs.getString("login_ip"));
		dto.setSina_id(rs.getString("sina_id"));
		dto.setQq_token(rs.getString("qq_token"));
		dto.setIs_fx(rs.getInt("is_fx"));
		dto.setReferral_count(rs.getInt("referral_count"));
		dto.setFocused_count(rs.getInt("focused_count"));
		dto.setUpdate_time(rs.getInt("update_time"));
		dto.setEmail(rs.getString("email"));
		dto.setCreate_time(rs.getInt("create_time"));
		dto.setWx_openid(rs.getString("wx_openid"));
		dto.setMobile(rs.getString("mobile"));
		dto.setUser_pwd(rs.getString("user_pwd"));
		dto.setArea_id(rs.getInt("area_id"));
		dto.setSex(rs.getInt("sex"));
		dto.setSohu_id(rs.getString("sohu_id"));
		dto.setT_access_token(rs.getString("t_access_token"));
		dto.setMerchant_name(rs.getString("merchant_name"));
		dto.setIs_syn_tencent(rs.getInt("is_syn_tencent"));
		dto.setId(rs.getInt("id"));
		dto.setCity_id(rs.getInt("city_id"));
		dto.setBday(rs.getInt("bday"));
		dto.setSina_token(rs.getString("sina_token"));
		dto.setQqv2_id(rs.getString("qqv2_id"));
		dto.setUser_pic(rs.getString("user_pic"));
		dto.setDev_type(rs.getString("dev_type"));
		dto.setIs_merchant(rs.getInt("is_merchant"));
		dto.setSina_app_secret(rs.getString("sina_app_secret"));
		dto.setBmonth(rs.getInt("bmonth"));
		dto.setPid(rs.getInt("pid"));
		dto.setLeft_times(rs.getInt("left_times"));
		dto.setIs_tmp(rs.getInt("is_tmp"));
		dto.setFx_total_money(rs.getDouble("fx_total_money"));
		dto.setVerify_create_time(rs.getInt("verify_create_time"));
		dto.setDaren_title(rs.getString("daren_title"));
		dto.setKaixin_id(rs.getString("kaixin_id"));
		dto.setStep(rs.getInt("step"));
		
		return dto;
	}
	
	/*****************************************绑定上下3级*********开始*******************************/
	/**
	 * fx_reward_link_record_table
	 * @param id
	 * @param pid
	 * @param pid1
	 * @param pid2
	 */
	public void bindingRelationship(int id,int pid,int pid1,int pid2){
		dao.transBegin();
		dao.exec("update "+userTable
				+ "	set pid="+pid+",pid1="+pid1+",pid2="+pid2+" where id="+id);
		Rs rs = new Rs(fx_reward_link_record_table);
		rs.set("fx_user_id", pid);
		rs.set("user_id", id);
		rs.set("is_done_fx", 0);
		dao.save(rs);
		dao.transCommit();
	}
	/*****************************************绑定上下3级*********结束*******************************/
}
